﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using Common.DataTypes;
using LINQAmazonProvider;
using System.Windows.Threading;
using System.Text.RegularExpressions;
using SilverlightApplication1.DataTypes;
using SilverlightApplication1;

namespace SilverlightApplication1
{
    public partial class Page : UserControl
    {       
        private Dispatcher _mainDispatcher;
        private List<Common.DataTypes.Book> lstQueryResult = new List<Book>();
        private Boolean blnAvanced;
        private DTSearchCriteria advanced;
        private int language;
        private string passwordAmazon;
        private Boolean passwordChanged;
        
        /***************************/
        private int totalPages;
        private int actualPage;
        private int totalItems;
        /***************************/

        // Atributo sobre el que ejecutaremos las busquedas
        // Amazon Search parameters
        private string searchKeyword; //TODO:: cambiar este atributo para que sea una estructura con toda la info
                                      //de la query a realizar.

        public List<Book> QueryResult
        {
            get
            {
                return lstQueryResult;
            }
            set
            {
                lstQueryResult = value;
            }
        }

        //TODO:: Cambiar el tipo de la Propiedad por la estructura a definir
        public DTSearchCriteria SearchCriteria { get; set; }
        

        public Page()
        {
            InitializeComponent();
            
            //TODO::Revisar comentario no es correcto y es confuso.
            // Se crea el Searcher y se crea el evento que maneja la finalizacion de la busqueda
            // Create searcher and event to handle search finalization event
            this._mainDispatcher = MainDispatcher.Current;
            //lstQueryResult = new List<Common.DataTypes.Item>();

            //This Boolean allows to manage the Advanced Search section
            blnAvanced = true;

            advanced = new DTSearchCriteria();

            passwordAmazon = "";
            passwordChanged = false;

            language = 1;    //English
            SetLanguage();

            txtQuery.TabIndex = 0;

            /***********************/
            //btnCloseMessage.Visibility = Visibility.Collapsed;
            icMoreInfoPopUp.Visibility = Visibility.Collapsed;
            SignInPopUp.Visibility = Visibility.Visible;
            PopPup();
            Show();
            /***********************/
        }

        #region Search Functions

        void SearcherFnc()
        {
            //TODO::Eliminar ya que no es necesario ya que no es siempre la misma lista
            //this.lstResult.Items.Clear();
            
            //TODO: Creacion de SearchCriteria
            // Amazon Search parameter
            //advanced.mainKeywords = this.txtQuery.Text;
            
            try
            {
                // Perform Amazon search
                //PerformSearch(keyword);

                // Create thread to perform the amazon search
                Thread worker = new Thread(new ThreadStart(GetLINQResult));

                //TODO:: Cambiar a set de Criteria objeto con toda la info de la busqueda. (No solo Keyword)
                // Set the static parameter Keyword
                SearchCriteria = advanced; //searchKeyword = keyword;
                worker.Start();
            }

            catch (Exception ex)
            {
                // TODO manejar excepcion
            }        
        }

        private void GetLINQResult()
        {
            try
            {
                //TODO::esto se tiene que ir
                List<Book> result = new List<Book>();

                var provider = new AmazonProvider();

                var queryable = new AmazonQuery<Book>(provider);

                if (SearchCriteria.isAdvanced)
                {
                    #region Advanced Search
                    int minPrice = decimal.ToInt32(SearchCriteria.minPrice);
                    int maxPrice = decimal.ToInt32(SearchCriteria.maxPrice);
                    string bookCondition = SearchCriteria.condition;

                    if (SearchCriteria.sortBy.isSortConditionSelected)
                    {
                        #region Sort criteria ON
                        if (SearchCriteria.category != DTSearchCriteria.NOTHING)
                        {
                            #region Sort criteria ON, Category criteria ON
                            if (SearchCriteria.maxPrice != 0)
                            {
                                #region Sort criteria ON, Category criteria ON, Maximum Price Criteria ON
                                //#region Sort criteria ON - LIST PRICE ASC, Category criteria ON, Maximum Price Criteria ON

                                if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                {
                                    #region Sort by at least List Price Ascending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Ascending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Ascending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Ascending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                              book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                {
                                    #region Sort by at least List Price Descending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Descending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Descending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Descending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                              book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice descending
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.PUB_DATE)
                                {
                                    #region Sort by at least Publication date
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Publication date, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Ascending, Title
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.PublicationDate, book.ListPrice, book.Title
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Ascending
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.PublicationDate, book.ListPrice
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Publication date, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Descending, Title
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending, book.Title
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Descending
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Publication date, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Publication date, Title, List Price Ascending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Publication date, Title, List Price Descending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice descending
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;

                                                    result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Publication date
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                              book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.PublicationDate
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region Sort by at least Title
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Title, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Ascending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Ascending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Title, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Descending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Descending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Title, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Title, Publication date, List Price Ascending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                         book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Title, Publication date, List Price Descending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                         book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice descending
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by Title, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Title
                                        var query =
                                           from book in queryable
                                           where
                                             book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                             book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                             book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                             book.Keywords == SearchCriteria.keywords
                                           orderby book.Title
                                           select book;

                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                #endregion
                            }
                            else
                            {
                                #region Sort criteria ON, Category criteria ON, Maximum Price Criteria OFF
                                //#region Sort criteria ON - LIST PRICE ASC, Category criteria ON, Maximum Price Criteria ON

                                if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                {
                                    #region Sort by at least List Price Ascending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Ascending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Ascending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Ascending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                              book.ListPrice.Amount >= minPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                {
                                    #region Sort by at least List Price Descending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Descending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Descending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Descending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                              book.ListPrice.Amount >= minPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice descending
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.PUB_DATE)
                                {
                                    #region Sort by at least Publication date
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Publication date, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Ascending, Title
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.PublicationDate, book.ListPrice, book.Title
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Ascending
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.PublicationDate, book.ListPrice
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Publication date, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Descending, Title
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending, book.Title
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Descending
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Publication date, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Publication date, Title, List Price Ascending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Publication date, Title, List Price Descending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice descending
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Publication date
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                              book.ListPrice.Amount >= minPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.PublicationDate
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region Sort by at least Title
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Title, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Ascending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Ascending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Title, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Descending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Descending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Title, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Title, Publication date, List Price Ascending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                         book.ListPrice.Amount >= minPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Title, Publication date, List Price Descending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                         book.ListPrice.Amount >= minPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice descending
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by Title, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Title
                                        var query =
                                           from book in queryable
                                           where
                                             book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                             book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                             book.ListPrice.Amount >= minPrice &&
                                             book.Keywords == SearchCriteria.keywords
                                           orderby book.Title
                                           select book;

                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            #region Sort criteria ON, Category criteria OFF
                            if (SearchCriteria.maxPrice != 0)
                            {
                                #region Sort criteria ON, Category criteria OFF, Maximum Price Criteria ON
                                //#region Sort criteria ON - LIST PRICE ASC, Category criteria ON, Maximum Price Criteria ON

                                if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                {
                                    #region Sort by at least List Price Ascending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Ascending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Ascending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Ascending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                              book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                {
                                    #region Sort by at least List Price Descending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Descending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Descending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Descending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                              book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice descending
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.PUB_DATE)
                                {
                                    #region Sort by at least Publication date
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Publication date, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Ascending, Title
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.PublicationDate, book.ListPrice, book.Title
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Ascending
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.PublicationDate, book.ListPrice
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Publication date, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Descending, Title
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending, book.Title
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Descending
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Publication date, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Publication date, Title, List Price Ascending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Publication date, Title, List Price Descending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice descending
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Publication date
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                              book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.PublicationDate
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region Sort by at least Title
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Title, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Ascending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Ascending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Title, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Descending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Descending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Title, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Title, Publication date, List Price Ascending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                         book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Title, Publication date, List Price Descending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                         book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice descending
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by Title, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Title
                                        var query =
                                           from book in queryable
                                           where
                                             book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                             book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                             book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                             book.Keywords == SearchCriteria.keywords
                                           orderby book.Title
                                           select book;

                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                #endregion
                            }
                            else
                            {
                                #region Sort criteria ON, Category criteria OFF, Maximum Price Criteria OFF
                                //#region Sort criteria ON - LIST PRICE ASC, Category criteria ON, Maximum Price Criteria ON

                                if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                {
                                    #region Sort by at least List Price Ascending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Ascending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Ascending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Ascending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Ascending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                              book.ListPrice.Amount >= minPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                {
                                    #region Sort by at least List Price Descending
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.PUB_DATE)
                                        {
                                            #region Sort by at least List Price Descending, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Publication date, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least List Price Descending, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by List Price Descending, Title, Publication date
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title, book.PublicationDate
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by List Price Descending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice descending, book.Title
                                                    select book;
                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by List Price Descending
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                              book.ListPrice.Amount >= minPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.ListPrice descending
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else if (SearchCriteria.sortBy.firstSortCondition == DTSearchCriteria.PUB_DATE)
                                {
                                    #region Sort by at least Publication date
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Publication date, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Ascending, Title
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.PublicationDate, book.ListPrice, book.Title
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Ascending
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.PublicationDate, book.ListPrice
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Publication date, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Publication date, List Price Descending, Title
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending, book.Title
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Publication date, List Price Descending
                                                var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.ListPrice descending
                                                        select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Publication date, Title
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Publication date, Title, List Price Ascending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Publication date, Title, List Price Descending
                                                    var query =
                                                        from book in queryable
                                                        where
                                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                          book.ListPrice.Amount >= minPrice &&
                                                          book.Keywords == SearchCriteria.keywords
                                                        orderby book.PublicationDate, book.Title, book.ListPrice descending
                                                        select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by List Price Ascending, Title
                                                var query =
                                                    from book in queryable
                                                    where
                                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                      book.ListPrice.Amount >= minPrice &&
                                                      book.Keywords == SearchCriteria.keywords
                                                    orderby book.ListPrice, book.Title
                                                    select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Publication date
                                        var query =
                                            from book in queryable
                                            where
                                              book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                              book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                              book.ListPrice.Amount >= minPrice &&
                                              book.Keywords == SearchCriteria.keywords
                                            orderby book.PublicationDate
                                            select book;
                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                else
                                {
                                    #region Sort by at least Title
                                    if (SearchCriteria.sortBy.isSecondConditionSelected)
                                    {
                                        #region Second Sort Condition Selected
                                        if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                        {
                                            #region Sort by at least Title, List Price Ascending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Ascending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Ascending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else if (SearchCriteria.sortBy.secondSortCondition == DTSearchCriteria.LST_PRICE_DSC)
                                        {
                                            #region Sort by at least Title, List Price Descending
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                #region Sort by Title, List Price Descending, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            else
                                            {
                                                #region Sort by Title, List Price Descending
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.ListPrice descending
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        else
                                        {
                                            #region Sort by at least Title, Publication date
                                            if (SearchCriteria.sortBy.isThirdConditionSelected)
                                            {
                                                if (SearchCriteria.sortBy.thirdSortCondition == DTSearchCriteria.LST_PRICE_ASC)
                                                {
                                                    #region Sort by Title, Publication date, List Price Ascending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                         book.ListPrice.Amount >= minPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                                else
                                                {
                                                    #region Sort by Title, Publication date, List Price Descending
                                                    var query =
                                                       from book in queryable
                                                       where
                                                         book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                         book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                         book.ListPrice.Amount >= minPrice &&
                                                         book.Keywords == SearchCriteria.keywords
                                                       orderby book.Title, book.PublicationDate, book.ListPrice descending
                                                       select book;

                                                    result = query.ToList<Book>();
                                                    #endregion
                                                }
                                            }
                                            else
                                            {
                                                #region Sort by Title, Publication date
                                                var query =
                                                   from book in queryable
                                                   where
                                                     book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                                     book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                                     book.ListPrice.Amount >= minPrice &&
                                                     book.Keywords == SearchCriteria.keywords
                                                   orderby book.Title, book.PublicationDate
                                                   select book;

                                                result = query.ToList<Book>();
                                                #endregion
                                            }
                                            #endregion
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region Sort by Title
                                        var query =
                                           from book in queryable
                                           where
                                             book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                             book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                             book.ListPrice.Amount >= minPrice &&
                                             book.Keywords == SearchCriteria.keywords
                                           orderby book.Title
                                           select book;

                                        result = query.ToList<Book>();
                                        #endregion
                                    }
                                    #endregion
                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    else
                    {
                        #region Sort criteria OFF
                        if (SearchCriteria.category != DTSearchCriteria.NOTHING)
                        {
                            #region Sort criteria OFF, Category criteria ON
                            if (SearchCriteria.maxPrice != 0)
                            {
                                #region Sort criteria OFF, Category criteria ON, Maximum Price Criteria ON
                                var query =
                                        from book in queryable
                                        where
                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                          book.Keywords == SearchCriteria.keywords
                                        select book;

                                result = query.ToList<Book>();
                                #endregion
                            }
                            else
                            {
                                #region Sort criteria OFF, Category criteria ON, Maximum Price Criteria OFF
                                var query =
                                        from book in queryable
                                        where
                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                          book.Title == SearchCriteria.title && book.Condition == bookCondition && book.Category == SearchCriteria.category &&
                                          book.ListPrice.Amount >= minPrice && book.Keywords == SearchCriteria.keywords
                                        select book;

                                result = query.ToList<Book>();
                                #endregion
                            }
                            #endregion
                        }
                        else
                        {
                            #region Sort criteria OFF, Category criteria OFF
                            if (SearchCriteria.maxPrice != 0)
                            {
                                #region Sort criteria OFF, Category criteria OFF, Maximum Price Criteria ON
                                var query =
                                        from book in queryable
                                        where
                                          book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                          book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                          book.ListPrice.Amount >= minPrice && book.ListPrice.Amount <= maxPrice &&
                                          book.Keywords == SearchCriteria.keywords
                                        select book;

                                result = query.ToList<Book>();
                                #endregion
                            }
                            else
                            {
                                #region Sort criteria OFF, Category criteria OFF, Maximum Price Criteria OFF
                                var query =
                                    from book in queryable
                                    where
                                      book.Author == SearchCriteria.author && book.Publisher == SearchCriteria.publisher &&
                                      book.Title == SearchCriteria.title && book.Condition == bookCondition &&
                                      book.ListPrice.Amount >= minPrice && book.Keywords == SearchCriteria.keywords
                                    select book;

                                result = query.ToList<Book>();
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    #endregion
                }
                else
                {
                    #region Normal Search
                    var query =
                    from book in queryable
                    where
                      book.Keywords == SearchCriteria.mainKeywords
                    select book;

                    result = query.ToList<Book>();
                    #endregion
                }
                
                // Set the advanced search to OFF, because when the first search is performed, advanced search windows is closed
                advanced.isAdvanced = false;

                //TODO::Revisar como se puede hacer esto hay que obtener el enumerator para que se
                //popule la query o se hace asi o se recorre la query evaluen el cliente pidio que no se generara
                //otra lista con el contenido de la query
                //query.GetEnumerator();
                //result = query.ToList<Book>();

                //TODO:esto se tiene que ir
                //foreach (var item in query)
                //{
                //    result.Add((Common.DataTypes.Book)item);
                //    lstQueryResult.Add((Common.DataTypes.Book)item);
                //}
                
                //Invoque the event on the UiThread
                _mainDispatcher.BeginInvoke(delegate()
                {
                    //OnSearchCompleted(new SearchCompletedEventArgs(result));
                    //TODO::Arreglar problema de tipos que retorna y aarreglar
                    SearchCompleted(result);
                });
            }
            catch (Exception e)
            {
                //TODO:: ver de hacer manejo de la excepcion o sacar
                throw e;
            }
        }

        private void SearchCompleted(IEnumerable<Book> query)
        {
            lstResult.ItemsSource = query;
            lstResult.Visibility = Visibility.Visible;

            hlnkAdvSearch.Foreground = new SolidColorBrush(Colors.Black);
            //'totPages' y 'totItems' deben ser los retornados por Amazon
            //InitializePagesInformation(totPages,totItems);
            InitializePagesInformation(10, 100);
        }

        void Search_Click(object sender, EventArgs args)
        {
            if (advanced.isAdvanced){
                advanced.author = uctrlAdvSearch.txtFilAuthor.Text;
                advanced.title = uctrlAdvSearch.txtFilTitle.Text;

                // Set the category
                if (uctrlAdvSearch.txtFilCategory.Tag == null)
                {
                    advanced.category = DTSearchCriteria.NOTHING;
                }
                else {
                    advanced.category = int.Parse(uctrlAdvSearch.txtFilCategory.Tag.ToString());
                }
                
                // Set the condition
                if (uctrlAdvSearch.rbtnCndAll.IsChecked.Value) {
                    advanced.condition = DTSearchCriteria.COND_All;
                }
                else if (uctrlAdvSearch.rbtnCndCollectible.IsChecked.Value) {
                    advanced.condition = DTSearchCriteria.COND_COLLECTIBLE;
                }
                else if (uctrlAdvSearch.rbtnCndRefurbished.IsChecked.Value)
                {
                    advanced.condition = DTSearchCriteria.COND_REFURBISHED;
                }
                else if (uctrlAdvSearch.rbtnCndUsed.IsChecked.Value)
                {
                    advanced.condition = DTSearchCriteria.COND_USED;
                }
                else {
                    advanced.condition = DTSearchCriteria.COND_NEW;
                }

                // Set min and max price
                //string textMaxPrice = uctrlAdvSearch.txtFilMaxPrice.Text;
                int textPriceValueAux;
                int.TryParse(uctrlAdvSearch.txtFilMaxPrice.Text, out textPriceValueAux);
                advanced.maxPrice = textPriceValueAux;

                int.TryParse(uctrlAdvSearch.txtFilMinPrice.Text, out textPriceValueAux);
                advanced.minPrice = textPriceValueAux;
                /*if (textMaxPrice.Equals(""))
                {
                    advanced.maxPrice = 0;
                }
                else {
                    advanced.maxPrice = int.Parse(textMaxPrice);
                }
                string textMinPrice = uctrlAdvSearch.txtFilMinPrice.Text;
                if (textMinPrice.Equals(""))
                {
                    advanced.maxPrice = 0;
                }
                else
                {
                    advanced.minPrice = int.Parse(textMinPrice);
                }*/
                
                advanced.publisher = uctrlAdvSearch.txtFilPublisher.Text;
                advanced.keywords = uctrlAdvSearch.txtFilKeyword.Text;
                
                int sortSelectedAuxValue;
                int.TryParse(uctrlAdvSearch.txtSortFirst.Tag.ToString(), out sortSelectedAuxValue);
                advanced.sortBy.isSortConditionSelected = (sortSelectedAuxValue != 0);
                if (advanced.sortBy.isSortConditionSelected)
                {
                    #region First sort parameter
                    if (sortSelectedAuxValue == 1)
                    { 
                        if (uctrlAdvSearch.rbtnPOrderAsc.IsChecked.Value){
                            advanced.sortBy.firstSortCondition = DTSearchCriteria.LST_PRICE_ASC;
                        }
                        else{
                            advanced.sortBy.firstSortCondition = DTSearchCriteria.LST_PRICE_DSC;
                        }
                    }
                    else if (sortSelectedAuxValue == 2)
                    {
                        advanced.sortBy.firstSortCondition = DTSearchCriteria.PUB_DATE;
                    }
                    else {
                        advanced.sortBy.firstSortCondition = DTSearchCriteria.TITLE;
                    }
                    #endregion

                    // Check if second sort parameter is selected, if true wich one
                    int.TryParse(uctrlAdvSearch.txtSortSecond.Tag.ToString(), out sortSelectedAuxValue);
                    advanced.sortBy.isSecondConditionSelected = (sortSelectedAuxValue != 0);
                    if (advanced.sortBy.isSecondConditionSelected)
                    {                       
                        #region Second sort parameter
                        if (sortSelectedAuxValue == 1)
                        {
                            if (uctrlAdvSearch.rbtnPOrderAsc.IsChecked.Value)
                            {
                                advanced.sortBy.secondSortCondition = DTSearchCriteria.LST_PRICE_ASC;
                            }
                            else
                            {
                                advanced.sortBy.secondSortCondition = DTSearchCriteria.LST_PRICE_DSC;
                            }
                        }
                        else if (sortSelectedAuxValue == 2)
                        {
                            advanced.sortBy.secondSortCondition = DTSearchCriteria.PUB_DATE;
                        }
                        else
                        {
                            advanced.sortBy.secondSortCondition = DTSearchCriteria.TITLE;
                        }
                        #endregion
                    }

                    // Check if second sort parameter is selected, if true wich one
                    int.TryParse(uctrlAdvSearch.txtSortThird.Tag.ToString(), out sortSelectedAuxValue);
                    advanced.sortBy.isSecondConditionSelected = (sortSelectedAuxValue != 0);
                    if (advanced.sortBy.isSecondConditionSelected)
                    {
                        #region Third sort parameter
                        if (sortSelectedAuxValue == 1)
                        {
                            if (uctrlAdvSearch.rbtnPOrderAsc.IsChecked.Value)
                            {
                                advanced.sortBy.thirdSortCondition = DTSearchCriteria.LST_PRICE_ASC;
                            }
                            else
                            {
                                advanced.sortBy.thirdSortCondition = DTSearchCriteria.LST_PRICE_DSC;
                            }
                        }
                        else if (sortSelectedAuxValue == 2)
                        {
                            advanced.sortBy.thirdSortCondition = DTSearchCriteria.PUB_DATE;
                        }
                        else
                        {
                            advanced.sortBy.thirdSortCondition = DTSearchCriteria.TITLE;
                        }
                        #endregion
                    }
                }
                /******************************/
                uctrlAdvSearch.Visibility = Visibility.Collapsed;
                txtQuery.Visibility = Visibility.Visible;
                txtQuery.Text = uctrlAdvSearch.txtFilKeyword.Text;
                hlnkAdvSearch.Content = "Advanced Search";
                btnCloseMessage_Click(sender, null);
                
            }
            else{
                advanced.mainKeywords = this.txtQuery.Text;
            }
            

            SearcherFnc();
        }

        #endregion

        #region Metodos viejos al finalizar la busqueda
        //private void OnSearchCompleted(SearchCompletedEventArgs e)
        //{
        //    searcher_SearchCompleted(this, e);
        //}
        
        //// Function to handle search complete event
        //void searcher_SearchCompleted(object sender, SearchCompletedEventArgs e)
        //{
        //    /*this.textprueba.Text = "DESPUES DE TERMINAR: ";

        //    Common.DataTypes.Book book;
        //    foreach (Common.DataTypes.Item i in e.Result)
        //    {
        //        book = (Common.DataTypes.Book)i;
        //        textprueba.Text += book.Author + " ";
        //    }*/
            
        //    Common.DataTypes.Book b;

        //    lstQueryResult.Clear();
        //    foreach (Common.DataTypes.Item i in e.Result)
        //    {
        //        //Add the Item to our local variable
        //        lstQueryResult.Add(i);
        //    }

        //    //In future this will be relative to the page
        //    for (int Index = 0; Index<5; Index++)
        //    {
        //        b = (Common.DataTypes.Book)lstQueryResult[Index];

        //        ListBoxItem item = new ListBoxItem();
        //        ListItemControl lstItemCtrl = new ListItemControl();
        //        lstItemCtrl.Tag = Index;
        //        lstItemCtrl.imgData.MouseLeftButtonDown += new MouseButtonEventHandler(ShowItemInformation);
        //        lstItemCtrl.imgData.Source = new BitmapImage(new Uri(b.MediumImage, UriKind.Absolute));
        //        lstItemCtrl.txtInfo1.Text = b.Author;
        //        lstItemCtrl.txtInfo2.Text = b.Title;
        //        lstItemCtrl.txtInfo3.Text = b.Publisher;
        //        lstItemCtrl.txtInfo4.Text = "Price: " + b.ListPrice.CurrencyCode + " " + b.ListPrice.FormattedPrice;
        //        item.Content = lstItemCtrl;
        //        lstResult.Items.Add(item);
        //    }

        //    lstResult.Visibility = Visibility.Visible;
        //}
        #endregion

        #region UI Methods
        void txtQueryKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                advanced.mainKeywords = this.txtQuery.Text;
                advanced.isAdvanced = false;
                SearcherFnc();
            }
        }

        Boolean showItemInfo = false;
        void ShowItemInformation(object sender, MouseButtonEventArgs e)
        {
            /*
             * The event SelectionChanged is executed before the event ShowItemInformation,
             * so at this moment we don't know which item contain the selected photo,
             * and for this reason here we simulate the event SelectionChanged.
             */
            showItemInfo = true;
            lstResultSelectionChanged(sender, null);
        }

        /***********************************************************************/
        void lstResultSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            List<Item> lst = new List<Item>();
            lst.Add((Item)lstResult.SelectedItem);
            icMoreInfoPopUp.ItemsSource = (IEnumerable<Item>)lst;
            if (showItemInfo)
            {
                SignInPopUp.Visibility = Visibility.Collapsed;
                icMoreInfoPopUp.Visibility = Visibility.Visible;
                PopPup();
                Show();
                showItemInfo = false;
            }
        }
        /***********************************************************************/

        void CloseItemInformation(object sender, EventArgs args)
        {
            //infoItemNotVisible.Begin();
            //icMoreInfo.Items.Clear();
        }
        
        void btnBooksToBigger(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderBigger.Stop();

            Storyboard.SetTargetName(renderBigger, "rdrBooks");
            rdrBooks.CenterX = 30;
            rdrBooks.CenterY = 30;
            renderBigger.Begin();
        }

        void btnBooksToLower(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderLower.Stop();

            Storyboard.SetTargetName(renderLower, "rdrBooks");
            renderLower.Begin();
        }

        void btnAbout1ToBigger(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderBigger.Stop();

            Storyboard.SetTargetName(renderBigger, "rdrAbout1");
            rdrAbout1.CenterX = 30;
            rdrAbout1.CenterY = 30;
            renderBigger.Begin();
        }

        void btnAbout1ToLower(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderLower.Stop();

            Storyboard.SetTargetName(renderLower, "rdrAbout1");
            renderLower.Begin();
        }

        void btnAbout2ToBigger(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderBigger.Stop();

            Storyboard.SetTargetName(renderBigger, "rdrAbout2");
            rdrAbout2.CenterX = 30;
            rdrAbout2.CenterY = 30;
            renderBigger.Begin();
        }

        void btnAbout2ToLower(object sender, EventArgs args)
        {
            // Case the storyboard is still executing on other button, it's stoped
            renderLower.Stop();

            Storyboard.SetTargetName(renderLower, "rdrAbout2");
            renderLower.Begin();
        }

        void Advanced(object sender, EventArgs args)
        {
            if (uctrlAdvSearch.Visibility == Visibility.Visible)
            {
                if (lstResult.Items.Count > 0)
                {
                    InformationVisbility(true);
                }
                uctrlAdvSearch.Visibility = Visibility.Collapsed;
                txtQuery.Visibility = Visibility.Visible;
                hlnkAdvSearch.Content = "Advanced Search";
                btnCloseMessage_Click(sender, null);
                advanced.isAdvanced = false;
            }
            else
            {
                InformationVisbility(false);
                if (sender.GetType().Equals(hlnkAdvSearch.GetType()))
                {
                    loadOptions();
                    uctrlAdvSearch.Visibility = Visibility.Visible;
                    txtQuery.Visibility = Visibility.Collapsed;
                    hlnkAdvSearch.Content = "Normal Search";
                    advanced.isAdvanced = true;
                    uctrlAdvSearch.SetFocus();
                }
                else
                {
                    if (ValidarDatos())
                    {
                        /*** SE SUPONE QUE AL IF NO ENTRA, PUES NO ESTA EN USO EL 'btnConfirm' ***/
                        if (((Button)sender).Name == btnConfirm.Name)
                        {
                            saveOptions();
                            advanced.isAdvanced = true;
                            SearcherFnc();
                        }
                        else
                        {
                            uctrlAdvSearch.Visibility = Visibility.Collapsed;
                            txtQuery.Visibility = Visibility.Visible;
                            hlnkAdvSearch.Content = "Advanced Search";
                            btnCloseMessage_Click(sender, null);
                        }
                    }
                    else
                    {
                        //show msgbox
                    }
                }
            }
            blnAvanced = !blnAvanced;
        }

        private void InformationVisbility(Boolean visible)
        {
            if (visible)
            {
                lstResult.Visibility = Visibility.Visible;
                lstResult.Visibility = Visibility.Visible;
                txtPagesInfoT.Visibility = Visibility.Visible;
                hlkFPageT.Visibility = Visibility.Visible;
                hlkPPageT.Visibility = Visibility.Visible;
                hlkNPageT.Visibility = Visibility.Visible;
                hlkLPageT.Visibility = Visibility.Visible;

                txtPagesInfoB.Visibility = Visibility.Visible;
                hlkFPageB.Visibility = Visibility.Visible;
                hlkPPageB.Visibility = Visibility.Visible;
                hlkNPageB.Visibility = Visibility.Visible;
                hlkLPageB.Visibility = Visibility.Visible;
            }
            else
            {
                lstResult.Visibility = Visibility.Collapsed;
                txtPagesInfoT.Visibility = Visibility.Collapsed;
                hlkFPageT.Visibility = Visibility.Collapsed;
                hlkPPageT.Visibility = Visibility.Collapsed;
                hlkNPageT.Visibility = Visibility.Collapsed;
                hlkLPageT.Visibility = Visibility.Collapsed;

                txtPagesInfoB.Visibility = Visibility.Collapsed;
                hlkFPageB.Visibility = Visibility.Collapsed;
                hlkPPageB.Visibility = Visibility.Collapsed;
                hlkNPageB.Visibility = Visibility.Collapsed;
                hlkLPageB.Visibility = Visibility.Collapsed;
            }
        }
        
        void btnConfirmToLight(object sender, EventArgs args)
        {
            imgDarkGreen.Visibility = Visibility.Collapsed;
            imgLightGreen.Visibility = Visibility.Visible;
        }

        void btnConfirmToDark(object sender, EventArgs args)
        {
            imgDarkGreen.Visibility = Visibility.Visible;
            imgLightGreen.Visibility = Visibility.Collapsed;
        }

        void btnCancelToLight(object sender, EventArgs args)
        {
            imgDarkRed.Visibility = Visibility.Collapsed;
            imgLightRed.Visibility = Visibility.Visible;
        }

        void btnCancelToDark(object sender, EventArgs args)
        {
            imgDarkRed.Visibility = Visibility.Visible;
            imgLightRed.Visibility = Visibility.Collapsed;
        }
        
        private void hlkSignIn_Click(object sender, RoutedEventArgs e)
        {
            //Aca iria una busqueda por defecto (que quizás pueda ser vacia) para chequear la password
            /*
            if (passwordAmazon es valida)
            {
                btnCloseMessage_Click(sender,e);
            }
            else
            {
                txtKeyPopUp.ClearValue();
                passwordAmazon.Remove(1,passwordAmazon.Length);
            }
             */
        }
        private void hlkSignInAgain_Click(object sender, RoutedEventArgs e)
        {
            //Aca iria una busqueda por defecto (que quizás pueda ser vacia) para chequear la password
            icMoreInfoPopUp.Visibility = Visibility.Collapsed;
            SignInPopUp.Visibility = Visibility.Visible;
            PopPup();
            Show();
        }
        #endregion    

        #region AdvancedSearch
        
        public void loadOptions(){
        
            //Sort by
            /*rbtPrice.IsChecked = (advanced.sortBy == DTSearchCriteria.LST_PRICE_ASC) ||
                                 (advanced.sortBy == DTSearchCriteria.LST_PRICE_DSC);
            rbtAsc.IsChecked = (advanced.sortBy == DTSearchCriteria.LST_PRICE_ASC);
            rbtDesc.IsChecked = (advanced.sortBy == DTSearchCriteria.LST_PRICE_DSC);
            rbtPublDate.IsChecked = (advanced.sortBy == DTSearchCriteria.PUB_DATE);
            rbtTitle.IsChecked = (advanced.sortBy == DTSearchCriteria.TITLE);*/
            
            //Filtered by
            //Author
            txtAuthor.Text = advanced.author;
            //Publisher
            txtPublisher.Text = advanced.publisher;
            //Title
            txtTitle.Text = advanced.title;
            //Category
            //TODO
            //Condition
            
            /* Comentado por Carlos... cpz que hay que volver a atras luego*/
            //chkUsed.IsChecked = (advanced.condition.Substring(DTSearchCriteria.COND_USED, 1) == "1");
            //chkCollectible.IsChecked = (advanced.condition.Substring(DTSearchCriteria.COND_COLLECTIBLE, 1) == "1");
            //chkRefurbished.IsChecked = (advanced.condition.Substring(DTSearchCriteria.COND_REFURBISHED, 1) == "1");
            /* Fin de comentario de Carlos */
            chkUsed.IsChecked = (advanced.condition.Equals(DTSearchCriteria.COND_USED));
            chkCollectible.IsChecked = (advanced.condition.Equals(DTSearchCriteria.COND_COLLECTIBLE));
            chkRefurbished.IsChecked = (advanced.condition.Equals(DTSearchCriteria.COND_REFURBISHED));

            //KeyWord
            txtKeyword.Text = advanced.keywords;
            //Max Price
            txtMaxPrice.Text = advanced.maxPrice > 0 ? advanced.maxPrice.ToString(): "";
            //Min Price
            txtMinPrice.Text = advanced.minPrice > 0 ? advanced.minPrice.ToString() : "";
             
        }
        
        public void saveOptions()
        {
            
            //Sort by
            /*if (rbtPrice.IsChecked.Value)
            {
                advanced.sortBy = rbtAsc.IsChecked.Value ? DTSearchCriteria.LST_PRICE_ASC : DTSearchCriteria.LST_PRICE_DSC;
            }
            else if (rbtPublDate.IsChecked.Value)
            {
                advanced.sortBy = DTSearchCriteria.PUB_DATE;
            }
            else if (rbtTitle.IsChecked.Value)
            {
                advanced.sortBy = DTSearchCriteria.TITLE;
            }
            else
            {
                advanced.sortBy = DTSearchCriteria.NOTHING;
            }*/

            
            //Filtered by

            //Author
            advanced.author = txtAuthor.Text.Trim();
            //Publisher
            advanced.publisher=txtPublisher.Text.Trim();
            //Title
            advanced.title = txtTitle.Text.Trim();
            //Category
            //TODO
            //Condition
            string Aux;
            Aux = chkUsed.IsChecked.Value ? "1" : "0";
            Aux += chkCollectible.IsChecked.Value ? "1" : "0";
            Aux += chkRefurbished.IsChecked.Value ? "1" : "0";
            advanced.condition = Aux;

            //KeyWord
            advanced.keywords = txtKeyword.Text.Trim();            
            //Max Price
            advanced.maxPrice= (txtMaxPrice.Text.Trim()!= "") ? int.Parse(txtMaxPrice.Text.Trim()):0 ;
            //Min Price
            advanced.minPrice = (txtMinPrice.Text.Trim() != "") ? int.Parse(txtMinPrice.Text.Trim()): 0;
            
        }

        public bool ValidarDatos()
        {
            Boolean validar = false;
            
            Regex reg = new Regex(@"^[0-9]*$");
            
            //Max Price is numeric?
            validar = reg.IsMatch(txtMaxPrice.Text.Trim()) || (txtMaxPrice.Text.Trim()=="");
            //Min Price is numeric?
            validar = validar & (reg.IsMatch(txtMinPrice.Text.Trim()) || (txtMinPrice.Text.Trim()==""));
            
            return validar;
        }

        #endregion

        #region Language

        void ChangeLanguage(object sender, EventArgs args)
        {
            //uctrlAdvSearch.btnClean_Click(sender,null);
            language = (language == 1) ? 2 : 1;
            SetLanguage();
        }

        private void SetLanguage()
        {
            TextLanguage textL = new TextLanguage();

            hlkSignIn.Content = textL.GetText(TextLanguage.fields.key_change_used, language);
            btnSearch.Content = textL.GetText(TextLanguage.fields.search, language);
            hlnkAdvSearch.Content = textL.GetText(TextLanguage.fields.adv_lnk, language);
            txtAmazonKeyPopUp.Text = textL.GetText(TextLanguage.fields.key_title, language);
            hlkSignInPopUp.Content = textL.GetText(TextLanguage.fields.key_signin, language);

            //Advanced Search
            uctrlAdvSearch.lblAdvTitle.Text = textL.GetText(TextLanguage.fields.adv_title, language);
            uctrlAdvSearch.lblAdvSort.Text = textL.GetText(TextLanguage.fields.adv_sortby, language);
            uctrlAdvSearch.lblAdvFiltered.Text = textL.GetText(TextLanguage.fields.adv_filteredby, language);
            uctrlAdvSearch.lblFilAuthor.Text = textL.GetText(TextLanguage.fields.adv_filt_author, language);
            uctrlAdvSearch.lblFilPublisher.Text = textL.GetText(TextLanguage.fields.adv_filt_publisher, language);
            uctrlAdvSearch.lblfilTitle.Text = textL.GetText(TextLanguage.fields.adv_filt_title, language);
            uctrlAdvSearch.lblfilCategory.Text = textL.GetText(TextLanguage.fields.adv_filt_category, language);
            uctrlAdvSearch.txtCatLF.Text = textL.GetText(TextLanguage.fields.adv_filt_cat_LF, language);
            uctrlAdvSearch.txtCatMedicine.Text = textL.GetText(TextLanguage.fields.adv_filt_cat_medicine, language);
            uctrlAdvSearch.txtCatMT.Text = textL.GetText(TextLanguage.fields.adv_filt_cat_MT, language);
            uctrlAdvSearch.txtCatNonfiction.Text = textL.GetText(TextLanguage.fields.adv_filt_cat_nonfiction, language);
            uctrlAdvSearch.txtCatON.Text = textL.GetText(TextLanguage.fields.adv_filt_cat_ON, language);
            uctrlAdvSearch.lblfilCondition.Text = textL.GetText(TextLanguage.fields.adv_filt_condition, language);
            uctrlAdvSearch.rbtnCndAll.Content = textL.GetText(TextLanguage.fields.adv_filt_cond_all, language);
            uctrlAdvSearch.rbtnCndUsed.Content = textL.GetText(TextLanguage.fields.adv_filt_cond_used, language);
            uctrlAdvSearch.rbtnCndCollectible.Content = textL.GetText(TextLanguage.fields.adv_filt_cond_coll, language);
            uctrlAdvSearch.rbtnCndRefurbished.Content = textL.GetText(TextLanguage.fields.adv_filt_cond_refu, language);
            uctrlAdvSearch.lblFilKeyword.Text = textL.GetText(TextLanguage.fields.adv_filt_keyword, language);
            uctrlAdvSearch.lblFilMaxPrice.Text = textL.GetText(TextLanguage.fields.adv_filt_maxprice, language);
            uctrlAdvSearch.lblFilMinPrice.Text = textL.GetText(TextLanguage.fields.adv_filt_minprice, language);

            uctrlAdvSearch.lblSortFirst.Text = textL.GetText(TextLanguage.fields.adv_sort_first, language);
            uctrlAdvSearch.lblSortSecond.Text = textL.GetText(TextLanguage.fields.adv_sort_second, language);
            uctrlAdvSearch.lblSortThird.Text = textL.GetText(TextLanguage.fields.adv_sort_third, language);

            uctrlAdvSearch.txtFLPrice.Text = textL.GetText(TextLanguage.fields.adv_sort_fLPrice, language);
            uctrlAdvSearch.txtFPublication.Text = textL.GetText(TextLanguage.fields.adv_sort_fPublication, language);
            uctrlAdvSearch.txtFTitle.Text = textL.GetText(TextLanguage.fields.adv_sort_fTitle, language);
            uctrlAdvSearch.txtSLPrice.Text = textL.GetText(TextLanguage.fields.adv_sort_sLPrice, language);
            uctrlAdvSearch.txtSPublication.Text = textL.GetText(TextLanguage.fields.adv_sort_sPublication, language);
            uctrlAdvSearch.txtSTitle.Text = textL.GetText(TextLanguage.fields.adv_sort_sTitle, language);
            uctrlAdvSearch.txtTLPrice.Text = textL.GetText(TextLanguage.fields.adv_sort_tLPrice, language);
            uctrlAdvSearch.txtTPublication.Text = textL.GetText(TextLanguage.fields.adv_sort_tPublication, language);
            uctrlAdvSearch.txtTTitle.Text = textL.GetText(TextLanguage.fields.adv_sort_tTitle, language);

            uctrlAdvSearch.ChangeTextComboBox(language);

            uctrlAdvSearch.hlkClean.Content = textL.GetText(TextLanguage.fields.adv_clean, language);

            // It shows the appropriate photo
            imgEEUU.Visibility = (language == 2) ? Visibility.Visible : Visibility.Collapsed;
            imgES.Visibility = (language == 1) ? Visibility.Visible : Visibility.Collapsed;
        }

        #endregion

        #region Password

        void txtKey_KeyDown(object sender, KeyEventArgs args)
        {
            passwordChanged = true;
        }

        void txtKey_TextChanged(object sender, TextChangedEventArgs args)
        {
            if (passwordChanged)
            {
                if (SignInPopUp.Visibility == Visibility.Visible)
                {
                    int Index = txtKeyPopUp.SelectionStart;

                    int Rest = txtKeyPopUp.Text.Length - passwordAmazon.Length;

                    if (Rest > 0)
                    {
                        passwordAmazon = passwordAmazon.Insert(txtKeyPopUp.SelectionStart - Rest, txtKeyPopUp.Text.Substring(txtKeyPopUp.SelectionStart - Rest, Rest));
                    }
                    else if (Rest == 0)
                    {
                        passwordAmazon = passwordAmazon.Substring(0, txtKeyPopUp.SelectionStart - 1)
                                      + txtKeyPopUp.Text.Substring(txtKeyPopUp.SelectionStart - 1, 1)
                                      + passwordAmazon.Substring(txtKeyPopUp.SelectionStart, passwordAmazon.Length - txtKeyPopUp.SelectionStart);

                    }
                    else
                    {
                        Rest = Rest * -1;
                        passwordAmazon = passwordAmazon.Remove(txtKeyPopUp.SelectionStart, Rest);
                    }

                    txtKeyPopUp.Text = new string('*', txtKeyPopUp.Text.Length);
                    txtKeyPopUp.SelectionStart = Index;
                    passwordChanged = false;

                }
            }
        }

        #endregion

        #region Paginated

        private void InitializePagesInformation(int totPages, int totItems)
        {
            if (totPages == 0)
            {
                //dependiendo del idioma, puede ser '...No hay resultados...'
                txtPagesInfoT.Text = "...No Results...";
            }
            else
            {
                actualPage = 1;
                totalItems = totItems;

                if (totPages == 1)
                {
                    totalPages = 1;
                    RefreshTxtPages(1, lstResult.Items.Count);

                    txtPagesInfoT.Visibility = Visibility.Visible;
                    txtPagesInfoB.Visibility = Visibility.Visible;
                }
                else
                {
                    // totPages > 1
                    totalPages = totPages;
                    RefreshTxtPages(1, 10);

                    txtPagesInfoT.Visibility = Visibility.Visible;
                    hlkFPageT.Visibility = Visibility.Collapsed;
                    hlkPPageT.Visibility = Visibility.Collapsed;
                    hlkNPageT.Visibility = Visibility.Visible;
                    hlkLPageT.Visibility = Visibility.Visible;

                    txtPagesInfoB.Visibility = Visibility.Visible;
                    hlkFPageB.Visibility = Visibility.Collapsed;
                    hlkPPageB.Visibility = Visibility.Collapsed;
                    hlkNPageB.Visibility = Visibility.Visible;
                    hlkLPageB.Visibility = Visibility.Visible;
                }
            }
        }

        private void hlkFPage_Click(object sender, RoutedEventArgs e)
        {
            actualPage = 1;
            /*
             * SUPONGO QUE TENDREMOS QUE PASARLES EL NUMERO DE ITEMS A SALTEAR, QUE EN ESTE CASO
             * SERIA 0, PARA QUE REALICEN LA BUSQUEDA POR DEFECTO
             */
            //SearcherFnc();

            RefreshTxtPages(1, 10);

            hlkFPageT.Visibility = Visibility.Collapsed;
            hlkPPageT.Visibility = Visibility.Collapsed;
            hlkNPageT.Visibility = Visibility.Visible;
            hlkLPageT.Visibility = Visibility.Visible;

            hlkFPageB.Visibility = Visibility.Collapsed;
            hlkPPageB.Visibility = Visibility.Collapsed;
            hlkNPageB.Visibility = Visibility.Visible;
            hlkLPageB.Visibility = Visibility.Visible;
        }

        private void hlkPPage_Click(object sender, RoutedEventArgs e)
        {
            actualPage -= 1;
            // PASANDO ((actualPage - 1) * 10)
            //SearcherFnc();

            int from = ((actualPage - 1) * 10) + 1;
            int to = actualPage * 10;

            hlkNPageT.Visibility = Visibility.Visible;
            hlkLPageT.Visibility = Visibility.Visible;
            hlkNPageB.Visibility = Visibility.Visible;
            hlkLPageB.Visibility = Visibility.Visible;

            if (actualPage == 1)
            {
                hlkFPageT.Visibility = Visibility.Collapsed;
                hlkPPageT.Visibility = Visibility.Collapsed;
                hlkFPageB.Visibility = Visibility.Collapsed;
                hlkPPageB.Visibility = Visibility.Collapsed;
            }
            
            RefreshTxtPages(from, to);
        }

        private void hlkNPage_Click(object sender, RoutedEventArgs e)
        {            
            // PASANDO (actualPage * 10)
            //SearcherFnc();

            actualPage += 1;

            int from = ((actualPage - 1) * 10) + 1;
            int to = (actualPage - 1) * 10;

            hlkFPageT.Visibility = Visibility.Visible;
            hlkPPageT.Visibility = Visibility.Visible;
            hlkFPageB.Visibility = Visibility.Visible;
            hlkPPageB.Visibility = Visibility.Visible;

            if (actualPage == totalPages)
            {
                to += lstResult.Items.Count;
                hlkNPageT.Visibility = Visibility.Collapsed;
                hlkLPageT.Visibility = Visibility.Collapsed;
                hlkNPageB.Visibility = Visibility.Collapsed;
                hlkLPageB.Visibility = Visibility.Collapsed;
            }
            else
            {
                to += 10;
            }
            RefreshTxtPages(from, to);
        }

        private void hlkLPage_Click(object sender, RoutedEventArgs e)
        {
            // PASANDO ((totalPages - 1) * 10)
            //SearcherFnc();

            actualPage = totalPages;

            int from = ((actualPage - 1) * 10) + 1;
            int to = ((actualPage - 1) * 10) + lstResult.Items.Count;

            hlkFPageT.Visibility = Visibility.Visible;
            hlkPPageT.Visibility = Visibility.Visible;
            hlkFPageB.Visibility = Visibility.Visible;
            hlkPPageB.Visibility = Visibility.Visible;

            hlkNPageT.Visibility = Visibility.Collapsed;
            hlkLPageT.Visibility = Visibility.Collapsed;
            hlkNPageB.Visibility = Visibility.Collapsed;
            hlkLPageB.Visibility = Visibility.Collapsed;
            
            RefreshTxtPages(from, to);
        }

        private void RefreshTxtPages(int from, int to)
        {
            //'of' o 'de' dependiendo del lenguaje

            txtPagesInfoT.Text = " " + from.ToString();
            txtPagesInfoT.Text += "-" + to.ToString();
            txtPagesInfoT.Text += " of " + totalItems.ToString() + " ";

            txtPagesInfoB.Text = " " + from.ToString();
            txtPagesInfoB.Text += "-" + to.ToString();
            txtPagesInfoB.Text += " of " + totalItems.ToString() + " ";
        }

        #endregion

        #region PopUp
        public void PopPup()
        {
            App.Current.Host.Content.Resized += new EventHandler(Content_Resized);
            backGround.Width = App.Current.Host.Content.ActualWidth;
            backGround.Height = App.Current.Host.Content.ActualHeight;
        }

        void Content_Resized(object sender, EventArgs e)
        {
            backGround.Width = App.Current.Host.Content.ActualWidth;
            backGround.Height = App.Current.Host.Content.ActualHeight;
        }

        void btnCloseMessage_Click(object sender, RoutedEventArgs e)
        {
            closeMessage.Begin();
        }

        public void Close()
        {
            popUpMessage.IsOpen = false;
            backGround.Visibility = Visibility.Collapsed;
        }

        public void Show()
        {
            Point p = new Point();
            p.X = App.Current.Host.Content.ActualWidth / 3;
            p.Y = App.Current.Host.Content.ActualHeight / 3;
            PopUpBox.Margin = new Thickness(p.X, p.Y, 0, 0);

            popUpMessage.IsOpen = true;
            backGround.Visibility = Visibility.Visible;
            openMessage.Completed += (s, e) =>
            {
                btnCloseMessage.Focus();
            };
            openMessage.Begin();
        }
        #endregion

    }

}
